package com.oa.dao.impl;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import com.oa.bean.work.Admin;
import com.oa.dao.AdminDAO;

/*
 * Session session = sessionFactory.openSession();
		Transaction tr = session.beginTransaction();
		try {
			
			tr.commit();
		} catch (Exception e) {
			e.printStackTrace();
			tr.rollback();
		} finally {
			session.close();
		}
 */

public class AdminDAOImpl implements AdminDAO {
	private SessionFactory sessionFactory;

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	@Override
	public void save(Admin admin) {
		Session session = sessionFactory.openSession();
		Transaction tr = session.beginTransaction();
		try {
			session.save(admin);
			tr.commit();
		} catch (Exception e) {
			e.printStackTrace();
			tr.rollback();
		} finally {
			session.close();
		}
	}

	@Override
	public void delete(Admin admin) {
		Session session = sessionFactory.openSession();
		Transaction tr = session.beginTransaction();
		try {
			session.delete(admin);
			tr.commit();
		} catch (Exception e) {
			e.printStackTrace();
			tr.rollback();
		} finally {
			session.close();
		}
	}

	@Override
	public Admin update(Admin admin) {
		Session session = sessionFactory.openSession();
		Transaction tr = session.beginTransaction();
		try {
			session.update(admin);
			tr.commit();
			return admin;
		} catch (Exception e) {
			e.printStackTrace();
			tr.rollback();
		} finally {
			session.close();
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Admin> queryAll() {
		Session session = sessionFactory.openSession();
		Transaction tr = session.beginTransaction();
		try {
			Criteria c = session.createCriteria(Admin.class);
			List<Admin> admins = c.list();
			tr.commit();
			return admins;
		} catch (Exception e) {
			e.printStackTrace();
			tr.rollback();
		} finally {
			session.close();
		}
		return null;
	}

	@Override
	public Admin queryById(Integer id) {
		Session session = sessionFactory.openSession();
		Transaction tr = session.beginTransaction();
		try {
			Admin a = (Admin) session.get(Admin.class, id);
			tr.commit();
			return a;
		} catch (Exception e) {
			e.printStackTrace();
			tr.rollback();
		} finally {
			session.close();
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Admin> queryByPager(int page, int pageSize) {
		Session session = sessionFactory.openSession();
		List<Admin> list = null;
		try {
			Criteria c = session.createCriteria(Admin.class);
			c.setFirstResult((page - 1) * pageSize);
			c.setMaxResults(pageSize);
			list = c.list();
		} catch (Exception e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		} finally {
			session.close();
		}
		return list;
	}

	@Override
	public Admin login(Admin admin) {
		Session session = sessionFactory.openSession();
		Transaction tr = session.beginTransaction();
		try {
			Query q = session.createQuery("from Admin where account=? and apwd=?");
			q.setString(0, admin.getAccount());
			q.setString(1, admin.getApwd());
			tr.commit();
			if (q.list() != null) {
				return (Admin) q.uniqueResult();
			}
		} catch (Exception e) {
			e.printStackTrace();
			tr.rollback();
		} finally {
			session.close();
		}
		return null;
	}

}
